package b2.widget.task
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.Label;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;

	public class TaskItem extends Sprite
	{
		public var taskIndex:int;
		private var _selected:Boolean;
		private var tabItemBackground:Sprite;
		private var tabItemBackground2:Sprite;
		private var statusLabel:Label;
		private const STATUS_NAME:Array = ["(不可接)", "(可接)", "(进行中)", "(完成)", "(完结)"];
		private const STATUS_COLOR:Array = [0xFF0000, 0x00AAAA, 0x00AA00, 0xAAAA00, 0x000000];
		
		public function TaskItem(taskIndex:int)
		{
			super();
			this.taskIndex = taskIndex;
			var taskConfig:TaskConfig = Config.taskConfigs[taskIndex];
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TaskItemBgr");
			tabItemBackground = new TabItemBackground();
			addChild(tabItemBackground);
			//背景框
			var TabItemBackground2:Class = ResourceHelper.getInstance().getClassByName("TaskItemBgr2");
			tabItemBackground2 = new TabItemBackground2();
			addChild(tabItemBackground2);
			//任务名字
			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
			nameText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0xFFFF00);
			nameText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			nameText.htmlText = taskConfig.name;
			nameText.x = 10;
			nameText.y = 5;
			addChild(nameText);
			//任务类型
			var typeText:TextField = new TextField();
			typeText.selectable = false;
        	typeText.autoSize = TextFieldAutoSize.LEFT;
			typeText.defaultTextFormat = new TextFormat("微软雅黑", 10, 0x000000);
			typeText.filters = [new GlowFilter(0xFFFFFF, 1, 1.2, 1.2, 100, BitmapFilterQuality.HIGH)];
			if (taskConfig.type == 0)
			{
				typeText.htmlText = "<font color='#00AA00'>[主线] </font>";
			}
			else if (taskConfig.type == 1)
			{
				typeText.htmlText = "<font color='#00AAAA'>[支线] </font>";
			}
			else if (taskConfig.type == 2)
			{
				if (taskConfig.typeName != "")
					typeText.htmlText = taskConfig.typeName;
				else
					typeText.htmlText = "<font color='#FF0000'>[节日] </font>";
			}
			typeText.x = 10;
			typeText.y = 25;
			addChild(typeText);
			refresh();
			selected = false;
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		public function refresh():void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			var taskStatus:int = me.getTaskStatus(taskIndex);
			if (statusLabel != null && statusLabel.parent != null)
				statusLabel.parent.removeChild(statusLabel);
			statusLabel = new Label(STATUS_NAME[taskStatus + 2], STATUS_COLOR[taskStatus + 2]);
			statusLabel.x = 200 - statusLabel.width;
			statusLabel.y = 12;
			//可接任务
			if (taskStatus == -1)
				statusLabel.blink = true;
			addChild(statusLabel);
		}
		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			tabItemBackground.visible = !value;
			tabItemBackground2.visible = value;
			this.filters = null;
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			if (!selected)
			{
				Mouse.cursor = "button";
				this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			}
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
		}
	}
}